import Vue from 'vue'
import Vuex from 'vuex'
import getters from './getters'

Vue.use(Vuex)
export default new Vuex.Store({
  state: {
    toastText: '',
    loadingVisible: false,
    currUser: {}
  },
  mutations: {
    increment (state) {
      state.count++
    },
    toast (state, text) {
      state.toastText = text
      setTimeout(() => {
        state.toastText = ''
      }, 2000)
    },
    loading (state, loadingVisible) {
      state.loadingVisible = loadingVisible
    },
    requireLogin (state) {
      const host = process.env.NODE_ENV === 'development' ? 'wxtest.zhichou.com' : 'wx.zhichou.com'
      location.assign('http://' + host + '/#/me/login?rurl=' + encodeURIComponent(location.href))
    },
    login(state, user) {
      console.log('Vuex被触发：', user)
      state.currUser = user;
      let userString = JSON.stringify(user.admin)
      localStorage.setItem("LoginAdmin", userString);
      let loginTime = new Date()
      loginTime = loginTime.getTime()
      localStorage.setItem('loginTime', loginTime)
    },
    logout(state, p2) {
      console.log('退出登录Vuex被触发：', p2)
      localStorage.removeItem("AdminInfo")
      localStorage.removeItem("AuthToken")
    }
  },
  getters
})
